package net.ycxinxing.mcpebox;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import net.ycxinxing.mcpebox.Pro.R;
import net.ycxinxing.mcpebox.mcpe.Level;
import net.ycxinxing.mcpebox.mcpe.MinecraftClient;
import net.ycxinxing.mcpebox.mcpe.Player;
import net.ycxinxing.mcpebox.utils.Utils;
import net.ycxinxing.mcpebox.widget.mTextView;

public class XFC
{
    private static int lastX,lastY,mScreenX=0,mScreenY=0;
    public static void showXFC(final Activity ctx)
    {
        final Runnable r=new Runnable(){
            @Override
            public void run()
            {
                final View view=ctx.getWindow().getDecorView();
                final ImageView contentView=new ImageView(ctx);
                contentView.setImageResource(R.drawable.xfc);
                final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);
                popupWindow.setWidth(120);
                popupWindow.setHeight(120);
                contentView.setOnTouchListener(new OnTouchListener(){
                        @Override
                        public boolean onTouch(View p1, MotionEvent event)
                        {
                            switch (event.getAction())
                            {
                                case MotionEvent.ACTION_DOWN: 
                                    //Toast.makeText(LaunchActivity.ctx,ScriptManager.nativeSetGameSpeed(+"",Toast.LENGTH_LONG).show();
                                    lastX = (int) event.getX();
                                    lastY = (int) event.getY();
                                    break;
                                case MotionEvent.ACTION_MOVE:
                                    mScreenX = (int) event.getRawX() - lastX;
                                    mScreenY = (int) event.getRawY() - lastY; 
                                    popupWindow.update(mScreenX, mScreenY, -1, -1, true); break; }
                            return false;
                        }

                    });
                contentView.setOnClickListener(new OnClickListener(){
                        @Override
                        public void onClick(View p1)
                        {
							AlertDialog.Builder ab=new AlertDialog.Builder(ctx);
							final List<String> list=new ArrayList<String>();
                            if (Level.currentLevel != null)
							{
								list.add("存档修改");
								list.add("玩家修改");
							}
							list.add("截屏");
							String arr[] = list.toArray(new String[0]);
							ab.setItems(arr, new DialogInterface.OnClickListener(){
									@Override
									public void onClick(DialogInterface p1, int p2)
									{
										switch (list.get(p2))
										{
											case "存档修改":
												存档修改(ctx);
												break;
											case "玩家修改":
												玩家修改(ctx);
												break;
											case "截屏":
												Utils.saveCurrentImage(ctx);
												break;
										}
										// TODO: Implement ctx method
									}
								});
							AlertDialog dlg=ab.show();
							Window win=dlg.getWindow();
							dlg.getListView().setBackgroundColor(Color.WHITE);
							//win.getDecorView().getLayoutParams().width=120;
							dlg.getListView().getLayoutParams().width = 360;
							//win.getDecorView().getLayoutParams().height=ViewGroup.LayoutParams.FILL_PARENT;
							win.setBackgroundDrawable(new Drawable(){
									@Override
									public void draw(Canvas p1)
									{
										// TODO: Implement this method
									}

									@Override
									public void setAlpha(int p1)
									{
										// TODO: Implement this method
									}

									@Override
									public void setColorFilter(ColorFilter p1)
									{
										// TODO: Implement this method
									}

									@Override
									public int getOpacity()
									{
										// TODO: Implement this method
										return 0;
									}
								});
							WindowManager.LayoutParams params = new WindowManager.LayoutParams();
							params.width = Utils.dip2px(ctx, 200);
							params.height = WindowManager.LayoutParams.WRAP_CONTENT;
							params.gravity = Gravity.LEFT | Gravity.TOP;
							win.setAttributes(params);
                            // TODO: Implement ctx method
                        }
                    });
                //popupWindow.setTouchable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                //popup)Window.showAsDropDown(view);
                popupWindow.showAtLocation(view, 0, 0, 0);
                // TODO: Implement ctx method
            }
        };
        ctx.runOnUiThread(new Runnable(){

                @Override
                public void run()
                {
                    new Handler().postDelayed(r, 500);
                    // TODO: Implement ctx method
                }
            });
        // TODO: Implement ctx method
    }
	private static void 玩家修改(final Context ctx)
	{
		AlertDialog.Builder dlg=new AlertDialog.Builder(ctx);
        ScrollView mainView=new ScrollView(ctx);
        LinearLayout l=new LinearLayout(ctx);
        l.setOrientation(1);
		//---------------------------------------------------------------------------
		LinearLayout gamemode=new LinearLayout(ctx);
        final Button cz=new Button(ctx);
        cz.setText("创造模式");
        final Button sc=new Button(ctx);
        sc.setText("生存模式");
        cz.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v)
                {
					MinecraftClient.currentClient.getLocalPlayer().setPlayerGameMode(1);
					MinecraftClient.currentClient.setGameType(1);
                    // TODO: Implement ctx method
                }
            });
        sc.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v)
                {
					MinecraftClient.currentClient.getLocalPlayer().setPlayerGameMode(0);
					MinecraftClient.currentClient.setGameType(0);
					
				}
				// TODO: Implement ctx method
            });
		gamemode.addView(cz);
		gamemode.addView(sc);
		gamemode.setOrientation(0);
		((LinearLayout.LayoutParams)cz.getLayoutParams()).width = 0;
		((LinearLayout.LayoutParams)cz.getLayoutParams()).weight = 1;
		((LinearLayout.LayoutParams)sc.getLayoutParams()).width = 0;
		((LinearLayout.LayoutParams)sc.getLayoutParams()).weight = 1;
		//---------------------------------------------------------------------------
		LinearLayout move=new LinearLayout(ctx);
		move.setOrientation(1);
		LinearLayout move_pos=new LinearLayout(ctx);
		move_pos.setOrientation(0);
		final TextInputEditText x=new TextInputEditText(ctx),y=new TextInputEditText(ctx),z=new TextInputEditText(ctx);
		x.setHint("x");
		y.setHint("y");
		z.setHint("z");
		x.setInputType(InputType.TYPE_CLASS_NUMBER);
		y.setInputType(InputType.TYPE_CLASS_NUMBER);
		z.setInputType(InputType.TYPE_CLASS_NUMBER);
		LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
		float[] pos=MinecraftClient.currentClient.getLocalPlayer().getPos();
		x.setLayoutParams(ll);
		x.setText("" + (int)pos[0]);
		y.setLayoutParams(ll);
		y.setText("" + (int)pos[1]);
		z.setLayoutParams(ll);
		z.setText("" + (int)pos[2]);
		move_pos.addView(x);
		move_pos.addView(y);
		move_pos.addView(z);
		move.addView(move_pos);
		Button btnmove=new Button(ctx);
		btnmove.setText("移动");
		move.addView(btnmove);
		btnmove.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					if (x.getText().toString().equals("") || y.getText().toString().equals("") || z.getText().toString().equals(""))
					{
						Toast.makeText(ctx, "坐标不得为空", Toast.LENGTH_SHORT).show();
					}
					else
					{
						try
						{
							Player localPlayer=MinecraftClient.currentClient.getLocalPlayer();
							//baseActivity.log(localPlayer.toString());
							localPlayer.setPos(Integer.parseInt(x.getText().toString()), Integer.parseInt(y.getText().toString()), Integer.parseInt(z.getText().toString()));
							Toast.makeText(ctx,"成功",Toast.LENGTH_SHORT).show();
						}
						catch (Exception e)
						{
							Toast.makeText(ctx, "失败", Toast.LENGTH_SHORT).show();
						}
					}
					// TODO: Implement this method
					// TODO: Implement this method
				}
			});
		//---------------------------------------------------------------------------
		//float i[]=NativeAPI.getLocalPlayerPos();
		l.addView(gamemode);
		l.addView(new mTextView(ctx, "瞬移"));
		l.addView(move);

		mainView.addView(l);
        dlg.setView(mainView);
        dlg.show();
		//Log.v("mcpebox", i.toString());
	}
    private static void 存档修改(final Context ctx)
    {
        AlertDialog.Builder dlg=new AlertDialog.Builder(ctx);
        ScrollView mainView=new ScrollView(ctx);
        LinearLayout l=new LinearLayout(ctx);
        l.setOrientation(1);
        //mainView.getLayoutParams().width=LayoutParams.MATCH_PARENT;
        final CheckBox TimeStopCb=new CheckBox(ctx);
        final SeekBar timeSb=new SeekBar(ctx);
        //---------------------------------------------------------------------------
        TimeStopCb.setText("锁定时间");
        TimeStopCb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton p1, boolean p2)
                {
                    Level.currentLevel.setStopTime(p2);
                    // TODO: Implement ctx method
                }
            });
        int tmp=Level.currentLevel.getStopTime();
        if (tmp != -1)
        {
            TimeStopCb.setChecked(true);
        }
        //---------------------------------------------------------------------------
        timeSb.setMax(24000);
        timeSb.setProgress(Level.currentLevel.getTime());
        timeSb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
                @Override
                public void onProgressChanged(SeekBar p1, int p2, boolean p3)
                {
                    //NativeAPI.setTime(p2);
					Level.currentLevel.setTime(p2);
                    TimeStopCb.setChecked(false);
                    // TODO: Implement ctx method
                }

                @Override
                public void onStartTrackingTouch(SeekBar p1)
                {
                    // TODO: Implement ctx method
                }

                @Override
                public void onStopTrackingTouch(SeekBar p1)
                {
                    //NativeAPI.setTime(p1.getProgress());
                    // TODO: Implement ctx method
                }
            });
        //---------------------------------------------------------------------------
        RadioGroup rg=new RadioGroup(ctx);
        rg.setOrientation(1);
        final RadioButton cz=new RadioButton(ctx);
        cz.setText("创造模式");
        final RadioButton sc=new RadioButton(ctx);
        sc.setText("生存模式");
        cz.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton p1, boolean p2)
                {
                    if (p2)
                    {
                        sc.setChecked(false);
                        Level.currentLevel.setDifaultGameType(1);
                    }
                    // TODO: Implement ctx method
                }
            });
        sc.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton p1, boolean p2)
                {
                    if (p2)
                    {
                        cz.setChecked(false);
                        Level.currentLevel.setDifaultGameType(0);
                    }
                    // TODO: Implement ctx method
                }
            });
        switch (Level.currentLevel.getDifaultGameType())
        {
            case 0:
                sc.setChecked(true);
                break;
            case 1:
                cz.setChecked(true);
                break;
        }
        rg.addView(cz);
        rg.addView(sc);
        //---------------------------------------------------------------------------
		Button setSpawnPos=new Button(ctx);
		setSpawnPos.setText("设置存档默认重生点为您的位置");
		setSpawnPos.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					float pos[]=MinecraftClient.currentClient.getLocalPlayer().getPos();
					Level.currentLevel.setLevelDifaultSpawn(pos[0], pos[1], pos[2]);
					Toast.makeText(ctx, "成功", Toast.LENGTH_SHORT).show();
					// TODO: Implement this method
				}
			});
		//---------------------------------------------------------------------------
		Button more=new Button(ctx);
		more.setText("更多");
		more.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					mDialog moredlg=new mDialog(ctx){
						@Override
						public void onButtonClick(int i)
						{
							存档修改_more(ctx, i);
							// TODO: Implement this method
						}
					};
					List<String> list=new ArrayList<String>();
					list.add("主世界");
					list.add("地狱");
					list.add("末地");
					moredlg.setList(list);
					moredlg.show();
					// TODO: Implement this method
				}
			});

		//---------------------------------------------------------------------------
		//int[] pos=NativeAPI.getLevelDifaultSpawn();
        l.addView(new mTextView(ctx, "时间"));
        l.addView(timeSb);
        l.addView(TimeStopCb);
        l.addView(rg);
		l.addView(setSpawnPos);
		l.addView(more);
        mainView.addView(l);
        dlg.setView(mainView);
        dlg.show();
    }
    public static void 存档修改_more(final Context ctx, final int level)
	{
		if (NativeAPI.CheckDimension(level))
		{
			LinearLayout l=new LinearLayout(ctx);
			l.setOrientation(1);
			AlertDialog.Builder dlg=new AlertDialog.Builder(ctx);
			ScrollView mainView=new ScrollView(ctx);
			//---------------------------------------------------------------------------
			SeekBar rain=new SeekBar(ctx);
			rain.setMax(9);
			rain.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
					@Override
					public void onProgressChanged(SeekBar p1, int p2, boolean p3)
					{
						NativeAPI.setLevelRain(level, p2);
						// TODO: Implement this method
					}

					@Override
					public void onStartTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}

					@Override
					public void onStopTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}
				});
			rain.setProgress(NativeAPI.getLevelRain(level));
			//---------------------------------------------------------------------------
			SeekBar light=new SeekBar(ctx);
			light.setMax(9);
			light.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
					@Override
					public void onProgressChanged(SeekBar p1, int p2, boolean p3)
					{
						NativeAPI.setLevelLight(level, p2);
						// TODO: Implement this method
					}

					@Override
					public void onStartTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}

					@Override
					public void onStopTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}
				});
			NativeAPI.getLevelLight(level);
			//---------------------------------------------------------------------------
			SeekBar fog=new SeekBar(ctx);
			fog.setMax(9);
			fog.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
					@Override
					public void onProgressChanged(SeekBar p1, int p2, boolean p3)
					{
						NativeAPI.setLevelFog(level, p2);
						// TODO: Implement this method
					}

					@Override
					public void onStartTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}

					@Override
					public void onStopTrackingTouch(SeekBar p1)
					{
						// TODO: Implement this method
					}
				});
			NativeAPI.getLevelFog(level);
			//---------------------------------------------------------------------------
			LinearLayout spawnPos=new LinearLayout(ctx);
			spawnPos.setOrientation(0);
			final TextInputEditText x=new TextInputEditText(ctx),y=new TextInputEditText(ctx),z=new TextInputEditText(ctx);
			x.setHint("x");
			y.setHint("y");
			z.setHint("z");
			x.setInputType(InputType.TYPE_CLASS_NUMBER);
			y.setInputType(InputType.TYPE_CLASS_NUMBER);
			z.setInputType(InputType.TYPE_CLASS_NUMBER);
			LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
			x.setLayoutParams(ll);
			y.setLayoutParams(ll);
			z.setLayoutParams(ll);
			spawnPos.addView(x);
			spawnPos.addView(y);
			spawnPos.addView(z);
			Button okSpawnPos=new Button(ctx);
			okSpawnPos.setText("设定重生点");
			okSpawnPos.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View p1)
					{
						if (x.getText().toString().equals("") || y.getText().toString().equals("") || z.getText().toString().equals(""))
						{
							Toast.makeText(ctx, "重生点不得为空", Toast.LENGTH_SHORT).show();
						}
						else
						{
							try
							{
								Level.currentLevel.setLevelDifaultSpawn(Integer.parseInt(x.getText().toString()), Integer.parseInt(y.getText().toString()), Integer.parseInt(z.getText().toString()));
								Toast.makeText(ctx, "设定成功", Toast.LENGTH_SHORT).show();
							}
							catch (Exception e)
							{
								Toast.makeText(ctx, "失败", Toast.LENGTH_SHORT).show();
							}
						}
						// TODO: Implement this method
					}
				});
			//---------------------------------------------------------------------------
			l.addView(new mTextView(ctx, "雨"));
			l.addView(rain);
			l.addView(new mTextView(ctx, "闪电"));
			l.addView(light);
			l.addView(new mTextView(ctx, "雾"));
			l.addView(fog);
			l.addView(spawnPos);
			l.addView(okSpawnPos);

			mainView.addView(l);
			dlg.setView(mainView);
			dlg.show();
		}
		else
		{
			Toast.makeText(ctx, "您没有创建过您所选择的存档", Toast.LENGTH_SHORT).show();
		}
	}
}
